package whu.cn.whushare.common;

import android.content.Context;

import com.androidplus.util.StringUtil;
import com.google.gson.reflect.TypeToken;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import whu.cn.whushare.bean.User;
import whu.cn.whushare.R;
import whu.cn.whushare.util.GsonUtil;

/**
 * 类 名：UserManager
 * 功 能：用户管理类，用于管理用户信息
 *       主要包括几个用于设置用户信息状态值的枚举类
 *       几个判断用户信息有效性的方法
 *       当前用户设置方法和登入登出操作
 */
public class UserManager {
	private static Context mContext;
	private static UserManager instance;
	private User mCurrentUser;

	private UserManager(Context context) {
		initContext(context);
	}

	public static UserManager getInstance(Context context) {
		if (instance == null) {
			synchronized (UserManager.class) {
				if (instance == null) {
					instance = new UserManager(context);
				}
			}
		}
		return instance;
	}

	public void initContext(Context context) {
		mContext = context.getApplicationContext();
		mCurrentUser = getLocalUserInfo();
		if (mCurrentUser == null)
			mCurrentUser = User.Null;
	}

    //用户信息状态类，采用枚举类型涵盖用户信息的各种不同状态
	public enum UserStatus {
		PHONE(0), EMAIL(1), QQ, SINA, NORMAL(2), ILLEGAL(mContext.getString(R.string.user_illegal)), EMPTY(mContext
				.getString(R.string.user_not_empty)),
        EMPTY2(mContext
                .getString(R.string.phone_not_empty)),  EMPTY3(mContext
                .getString(R.string.mail_not_empty)),PHONE_ILLEGAL(mContext.getString(R.string.phone_illegal)),
        EMAIL_ILLEGAL(mContext.getString(R.string.mail_illegal)), LESS(mContext
                .getString(R.string.user_name_less));

		private String msg;
		private int type;
       // public static final int MIN_USER_NAME_LENGTH =4;

		public int getType() {
			return type;
		}

		public String getMsg() {
			return msg;
		}

		UserStatus() {
		}

		UserStatus(String str) {
			msg = str;
		}

		UserStatus(int i) {
			type = i;
		}
	}

    //密码状态类，采用枚举类型涵盖密码设置过程中的不同状态
	public enum PassStatus {
		EMPTY(mContext.getString(R.string.pass_not_empty)), NORMAL, WEAK, STRONG, LESS(mContext
				.getString(R.string.pass_less));

		public static final int MIN_PASS_LENGTH =16;
		private String msg;

		public String getMsg() {
			return msg;
		}

		PassStatus() {
		}

		PassStatus(String msg) {
			this.msg = msg;
		}
	}

    //昵称状态类
	public enum NickNameStatus {
        EMPTY(mContext
                .getString(R.string.nickname_not_empty)), NORMAL, ILLEGAL(mContext.getString(R.string.user_illegal));

		private String msg;

		public String getMsg() {
			return msg;
		}

		NickNameStatus() {
		}

		NickNameStatus(String msg) {
			this.msg = msg;
		}
	}

    //验证码状态类
	public enum IdCodeStatus {
		EMPTY(mContext.getString(R.string.id_code_not_empty)), NORMAL, ILLEGAL(mContext
				.getString(R.string.id_code_illegal));

		private String msg;

		public String getMsg() {
			return msg;
		}

		IdCodeStatus() {
		}

		IdCodeStatus(String msg) {
			this.msg = msg;
		}
	}

    //判断字符串str是否为正确的电话号码，是则返回true；否则返回false；
	public boolean isPhoneNum(String str) {
		Pattern pattern = Pattern.compile("^(13|15|18)\\d{9}$");
		Matcher matcher = pattern.matcher(str);

			return matcher.matches();
	}

    //判断字符串str是否为正确的邮箱地址，是则返回true；否则返回false；
	public boolean isMail(String str) {
		Pattern pattern = Pattern.compile("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\\.([a-zA-Z0-9_-])+)+$");
		Matcher matcher = pattern.matcher(str);

		if (matcher.matches()) {
			return true;
		}
		return false;
	}

    /**
     * 判断登录用户名有效性
     * @param str - 待判断的登录用户名
     * @return 登录用户名状态，
     *          若为空，返回“用户名不能为空”;{@link UserStatus#EMPTY}
     *          若为电话号码，返回0;{@link UserStatus#PHONE}
     *          若为邮箱地址，返回1;{@link UserStatus#EMAIL}
     *          否则为可用的用户名，返回2;{@link UserStatus#NORMAL}
     */
	public UserStatus validateUserName(String str) {
		if (StringUtil.isNullOrEmpty(str))
			return UserStatus.EMPTY;
		else if (isPhoneNum(str))
			return UserStatus.PHONE;
		else if (isMail(str))
			return UserStatus.EMAIL;
     /*   else if (str.length() < UserStatus.MIN_USER_NAME_LENGTH)
            return UserStatus.LESS;*/
        else
			return UserStatus.NORMAL;
	}

    /**
     * 判断用户昵称的有效性
     * @param name - 待判断的用户昵称
     * @return 用户昵称状态，
     *          若为空，返回“昵称不能为空”;{@link NickNameStatus#EMPTY}
     *          若为有效昵称，NORMAL;{@link NickNameStatus#NORMAL}
     *          否则为无效昵称，返回“用户名非法”;{@link NickNameStatus#ILLEGAL}
     */
	public NickNameStatus validateNickName(String name) {
		Pattern pattern = Pattern.compile("^(?![0-9]+$)[0-9A-Za-z\\u4e00-\\u9fa5]+$");
		Matcher matcher = pattern.matcher(name);
		return StringUtil.isNullOrEmpty(name) ? NickNameStatus.EMPTY : matcher.matches() ? NickNameStatus.NORMAL
				: NickNameStatus.ILLEGAL;
	}

    /**
     * 判断手机号的有效性
     * @param str - 待判断的手机号
     * @return  手机号状态，
     *          若为空，返回“手机号不能为空”;{@link UserStatus#EMPTY2}
     *          若为有效手机号，0;{@link UserStatus#PHONE}
     *          否则为无效手机号，返回“手机号格式不正确”;{@link UserStatus#PHONE_ILLEGAL}
     */
	public UserStatus validatePhone(String str) {
		if (StringUtil.isNullOrEmpty(str))
			return UserStatus.EMPTY2;
		else if (isPhoneNum(str))
			return UserStatus.PHONE;
		else
			return UserStatus.PHONE_ILLEGAL;
	}

    /**
     * 判断邮箱的有效性
     * @param str - 待判断的邮箱
     * @return  邮箱状态，
     *          若为空，返回“邮箱不能为空”;{@link UserStatus#EMPTY3}
     *          若为有效邮箱，1;{@link UserStatus#EMAIL}
     *          否则为无效邮箱，返回“邮箱格式不正确”;{@link UserStatus#EMAIL_ILLEGAL}
     */
	public UserStatus validateMail(String str) {
		if (StringUtil.isNullOrEmpty(str))
			return UserStatus.EMPTY3;
		else if (isMail(str))
			return UserStatus.EMAIL;
		else
			return UserStatus.EMAIL_ILLEGAL;
	}

    /**
     * 判断密码的有效性
     * @param str - 待判断的密码
     * @return  密码状态，
     *          若为空，返回“密码不能为空”;{@link PassStatus#EMPTY}
     *          若密码超长，返回“密码必须小于16位字符！”;{@link PassStatus#LESS}
     *          否则为有效密码，返回NORMAL;{@link PassStatus#NORMAL}
     */
	public PassStatus validatePass(String str) {
		if (StringUtil.isNullOrEmpty(str))
			return PassStatus.EMPTY;
		else if (str.length() >= PassStatus.MIN_PASS_LENGTH)
			return PassStatus.LESS;
		else
			return PassStatus.NORMAL;
	}

    /**
     * 判断验证码的有效性
     * @param str - 待判断的验证码
     * @return  验证码状态，
     *          若为空，返回“验证码不能为空”;{@link IdCodeStatus#EMPTY}
     *          否则返回NORMAL;{@link IdCodeStatus#NORMAL}
     */
	public IdCodeStatus validateIdCode(String str) {
		if (StringUtil.isNullOrEmpty(str))
			return IdCodeStatus.EMPTY;
		else
			return IdCodeStatus.NORMAL;
	}

    /**
     * 判断用户是否登录
     * @return 若用户已登录，返回true；否则返回false
     */
	public boolean hasLogin() {
		return mCurrentUser != null && !mCurrentUser.equals(User.Null);
	}

    /**
     * 获取当前用户
     * @return 若用户已登录，返回当前登录用户；否则返回匿名用户;{@link User#Null}
     */
	public User getCurrentUser() {
		return hasLogin() ? mCurrentUser : User.Null;
	}

    //设置当前登录用户
	public void setCurrentUser(User mCurrentUser) {
		this.mCurrentUser = mCurrentUser;
	}

    //登入
	public void login(User user) {
		mCurrentUser = user;
		SPManager.getInstance(mContext).saveUserInfo(user);
	}

    //登出
	public void logout() {
		if (!mCurrentUser.equals(User.Null)) {
			mCurrentUser = User.Null;
			SPManager.getInstance(mContext).removeUserInfo();
		}
	}

    //获取当前用户信息
	public User getLocalUserInfo() {
		if (mCurrentUser == null)
			mCurrentUser = User.Null;

		if (mCurrentUser.equals(User.Null)) {
			String userString = SPManager.getInstance(mContext).getUserInfo();
			if (!StringUtil.isNullOrEmpty(userString)) {
                //从json字符串userString生成一个User对象
				mCurrentUser = GsonUtil.fromJsonSimple(userString, new TypeToken<User>() {
				}.getType());
			}
		}
		return mCurrentUser;
	}
}
